package com.globalwave.pos;

import org.apache.commons.lang.StringUtils;

import com.globalwave.common.cache.CodeHelper;
import com.globalwave.common.exception.BusinessException;
import com.globalwave.db.seq.Sequence;
import com.globalwave.db.seq.impl.TableSequence;
import com.globalwave.pos.payment.entity.Currency;

public class POSCodeHelper extends CodeHelper {

	/**
	 * 判断是否使用内部帐户的余额
	 * 
	 * @return
	 */
	public static boolean isUseInnerAccount(String current_code) {
		short currency_type = 0;
		try {
			currency_type = CodeHelper.getShort("CURRENCY", "type_", current_code);
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		
		return (Currency.TYPE_INNER_ACCOUNT == currency_type);
	}
	
	public static long getOid(Short orderType, Integer saleDate) {
		int yearMonth = (saleDate/100) % 10000 ;
		long prefix = orderType * 10000 + yearMonth ;
		
		String seqName = "POS$" + StringUtils.leftPad(String.valueOf(prefix), 6, "0") ;
		Sequence seq = null ;
		//try {
			seq = new TableSequence(seqName) ;
			long next = seq.nextVal() ;
			if (next == -1L) {
				next = seq.create() ;
			}
			
			return prefix * 100000000 + next ;
			/*
		} catch (Exception e) {// may be not exists
			
			LogFactory.getLog(POSCodeHelper.class).error(e, e) ;
			
			return prefix * 100000000 + seq.create() ;
		}*/
	}
}
